<?php

class DataOptions {
    
    private $name = '';
    private $params = array();
    private $modul;
    
    function __construct($name, $param, $modul) {
        
        $this->name = $name;
        $this->params = $param;
        $this->modul = $modul;
    }
    
    
    public function getParametrs($method) {
        
        if (!isset($this->modul->parametrs[$method]['parametrs'])) {
            
            return array();
        }
        
        return $this->create($this->modul->parametrs[$method]['parametrs']);
    }
    
    
    public function getWidgets($method) {
        
        if (!isset($this->modul->widgets[$method]['parametrs'])) {
            
            return array();
        }
        
        return $this->create($this->modul->widgets[$method]['parametrs']);
    }
    
    
    private function create($array) {
        
        $end = array();
        
        foreach($array as $name => $row) {
            
            $value  = '';
            $method = 'field_'.$row[1];
            
            if (!method_exists($this, $method)) continue;
            
            if (isset($row[2]) && method_exists($this->modul, $row[2])) {
                
                $value = $this->modul->$row[2]();
            }
            
            $end[$row[0]] = $this->$method($name, $value);
        }
        
        return $end;
    }
        
    
    private function field_input($name) {
        
        if (isset($this->params[$name])) {
            
            return form_input($this->name.'['.$name.']', $this->params[$name]);
        }
        
        return form_input($this->name.'['.$name.']');
    }
    
    
    private function field_checkbox($name) {
        
        if (isset($this->params[$name])) {
            
            return form_checkbox($this->name.'['.$name.']', 1, $this->params[$name]);
        }
        
        return form_checkbox($this->name.'['.$name.']', 1);
    }
    
    
    private function field_select($name, $value = array()) {
        
        if (isset($this->params[$name])) {
            
            return form_dropdown($this->name.'['.$name.']', $value, $this->params[$name]);
        }
        
        return form_dropdown($this->name.'['.$name.']', $value);
    }
    
    
    private function field_doubl_input($name) {
        
        $return = '<br><br><span id="widget_list_menu">';
                    
        if (isset($this->params[$name]['key']) && count($this->params[$name]['key']) > 0) {
            
            $i = 0;
            foreach($this->params[$name]['key'] as $k => $v) {
                
                $return .= '<span>';
                $return .= js_anchor("", image('images/icons/arrow-move.png'), array('id' => 'doublwmov')).' ';
                $return .= form_input($this->name.'['.$name.'][key][]', $v).' ';
                $return .= form_input($this->name.'['.$name.'][value][]', $this->params[$name]['value'][$k]);
                
                $return .= ' '.js_anchor("widget_doubl_input(this.id)", image('images/icons/plus.png'), array('id' => 'doublw_'.$name.$i));
                $return .= ' '.js_anchor("widget_doubl_input_del(this.id)", image('images/icons/cross.png'), array('id' => 'doublwdel_'.$name.$i));
                $return .= '</span>';
                
                $i++;
            }
        }
        else {
            
            $return .= '<span>';
            $return .= js_anchor("", image('images/icons/arrow-move.png'), array('id' => 'doublwmov')).' ';
            $return .= form_input($this->name.'['.$name.'][key][]').' ';
            $return .= form_input($this->name.'['.$name.'][value][]');
            
            $return .= ' '.js_anchor("widget_doubl_input(this.id)", image('images/icons/plus.png'), array('id' => 'doublw_'.$name));
            $return .= ' '.js_anchor("widget_doubl_input_del(this.id)", image('images/icons/cross.png'), array('id' => 'doublwdel_'.$name));
            $return .= '</span>';
        }
        
        $return .= '</span><script>widget_doubl_input_mov();</script>';
        
        return $return;
    }
}


class Adminmodel extends Model {

    function Adminmodel()
    {
        parent::Model();
    }
    
    
    //---Pages---//
    
    public function page_edite($id, $parent_id, $uri, $title, $modul, $method, $mask, $parametrs, $template, $global_template, $description = NULL, $keywords = NULL, $settings = array()) {
        
        if ($uri == 'www') $parent_id = 0;
        
        $data = array(
                'parent_id'         => $parent_id,
                'uri'               => $uri,
                'title'             => $title,
                'modul'             => $modul,
                'method'            => $method,
                'mask'              => $mask,
                'parametrs'         => $parametrs,
                'template'          => $template,
                'global_template'   => $global_template,
                'description'       => $description,
                'keywords'          => $keywords,
                'settings'          => (count($settings) == 0) ? NULL : serialize($settings)
            );

        $this->db->update('pages', $data, array('id' => $id)); 
    }
    
    public function page_add($parent_id, $uri, $title, $modul, $method, $mask, $parametrs, $template, $global_template, $description = NULL, $keywords = NULL, $settings = array()) {
        
        $data = array(
                'parent_id'         => $parent_id,
                'uri'               => $uri,
                'title'             => $title,
                'modul'             => $modul,
                'method'            => $method,
                'mask'              => $mask,
                'parametrs'         => $parametrs,
                'template'          => $template,
                'global_template'   => $global_template,
                'description'       => $description,
                'keywords'          => $keywords,
                'settings'          => (count($settings) == 0) ? NULL : serialize($settings)
            );

        $this->db->insert('pages', $data); 
    }
    
    public function page_del($uri) {
        
        $this->db->delete('pages', array('uri' => $uri)); 
    }
    
    public function get_page_form_params($method, $modul, $params = '') {
        
        $params = ($params != '' || $params !== false) ? unserialize($params) : array();
        
        $data = new DataOptions('settings', $params, $modul);
        return $data->getParametrs($method);
    }
    
    
    //---Templates---//
    
    public function get_templates() {
        
        $templates = dir(dirname(FCPATH).'/templates/');
        $res = array();
        
        while (false !== ($template = $templates->read())) {
          
            if($template != '.' && $template != '..'){
                
                $res[$template] = $template;
            }          
        }
        
        $templates->close();
        
        return $res;
    }
    
    public function get_template_files($template) {
        
        $directory = @dir(dirname(FCPATH).'/templates/'.$template.'/');
        $res = array();
        
        while (false !== ($temp = $directory->read())) {
          
            if( substr($temp, -4) == '.php' && $temp != 'footer.php' && $temp != 'header.php' ){
                
                $res[substr($temp, 0, -4)] = $temp;
            }     
        }
        
        $directory->close();
        
        return $res;
    }
    
    
    //---Widgets---//
    
    public function get_widget_containers() {
        
        return array(
            'sidebar1' => $this->lang->line('widget_sidebar1'),
            'sidebar2' => $this->lang->line('widget_sidebar2'),
            'sidebar3' => $this->lang->line('widget_sidebar3'),
            'sidebar4' => $this->lang->line('widget_sidebar4'),
        
            'header1' => $this->lang->line('widget_header1'),
            'header2' => $this->lang->line('widget_header2'),
            'header3' => $this->lang->line('widget_header3'),
            'header4' => $this->lang->line('widget_header4'),
        
            'menu1' => $this->lang->line('widget_menu1'),
            'menu2' => $this->lang->line('widget_menu2'),
            'menu3' => $this->lang->line('widget_menu3'),
            'menu4' => $this->lang->line('widget_menu4'),
        
            'footer1' => $this->lang->line('widget_footer1'),
            'footer2' => $this->lang->line('widget_footer2')
        );
    }
    
    
    public function get_widget_form_params($name, $modul, $params = '') {
        
        $result = array();
        $params = ($params != '') ? unserialize($params) : array();
        
        $data = new DataOptions('widget', $params, $modul);
        return $data->getWidgets($name);
    }
    
    
    public function add_widget($container, $title, $modul, $method, $parametrs, $order, $cache) {
        
        $data['container'] = $container;
        $data['title'] = $title;
        $data['modul'] = $modul;
        $data['method'] = $method;
        $data['order'] = $order;
        $data['cache'] = $this->widget->create_cache_parametrs($cache);
        
        if (is_array($parametrs) && count($parametrs) > 0) {
            
            $data['parametrs'] = serialize($parametrs);
        }

        $this->db->insert('widgets', $data);
    }
    
    public function get_widget($id = false) {
        
        if ($id != false) {
            
            $this->db->where('id', $id); 
        }
        else {
            
            $this->db->order_by("container", "asc");
            $this->db->order_by("order", "asc");
        }        
        return $this->db->get('widgets');
    }
    
    public function del_widget($id) {
        
        $this->db->delete('widgets', array('id' => $id));
    }
    
    public function edite_widget($id, $container, $title, $parametrs, $order, $cache) {
        
        $data['container'] = $container;
        $data['title'] = $title;
        $data['order'] = $order;
        $data['cache'] = $this->widget->create_cache_parametrs($cache);
        
        if (is_array($parametrs) && count($parametrs) > 0) {
            
            $data['parametrs'] = serialize($parametrs);
        }
        
        $this->db->update('widgets', $data, array('id' => $id));
    }
    
    public function edite_widget_visible($id, $params = array()) {
        
        if (is_array($params) && count($params) > 0) {
        
            $data['pages'] = serialize($params);
        }
        else {
            
            $data['pages'] = NULL;
        }
        
        $this->db->update('widgets', $data, array('id' => $id));
    }
    
    
    //---Groups and Rights---//
    
    public function get_groups() {
        
        return $this->db->get('groups');
    }
    
    public function get_rights($page) {
        
        $this->db->join('groups', 'groups.id = rights.role');
        return $this->db->get_where('rights', array('uri' => $page));
    }
    
    public function edite_rights($page, $array) {
        
        $groups = $this->get_groups()->result_array();
        
        foreach($groups as $row) {
            
            $data['is_view']  = (isset($array[$row['id']]['is_view']))  ? 1 : 0;
            $data['is_write'] = (isset($array[$row['id']]['is_write'])) ? 1 : 0;
            $data['is_admin'] = (isset($array[$row['id']]['is_admin'])) ? 1 : 0;
            
            $this->db->update('rights', $data, array('uri' => $page, 'role' => $row['id']));
        }
    }
    
    public function add_rights($page, $array) {
        
        $groups = $this->get_groups()->result_array();
        
        foreach($groups as $row) {
            
            $data = (!isset($array[$row['id']])) ? array('is_view' => 0, 'is_write' => 0, 'is_admin' => 0) : $array[$row['id']];
            $data['uri'] = $page;
            $data['role'] = $row['id'];
            
            $this->db->insert('rights', $data);
        }
    }
    
    public function del_rights($page) {
        
        $this->db->delete('rights', array('uri' => $page)); 
    }
    
    
    //---Events---//
    
    public function event_reset_cache() {
        
        Cache::clearGroup('core');
        Cache::disable();
    }
}